window._FIGHTAI = {

    _SKILLCACHE:null,

    getArmyLiveNmb:function(){
        var nmb=0;
        for(var i=0;i<window.fightData.armyTeam.length;i++){
            if(window.fightData.armyTeam[i].hp>0)
                nmb++;
        }

        return nmb;
    },

    isArmyDie:function(){
        for(var i=0;i<window.fightData.armyTeam.length;i++){
            if(window.fightData.armyTeam[i].hp<=0)
                return true;
        }

        return false;
    },

    getActionCmd:function(doactionPlayer){

        //console.log('当前行动人:',doactionPlayer);

        //最终boss专属技能机制
        if(!doactionPlayer.isplayer&&doactionPlayer.name=="九天玄女"){


            var livenmb = _FIGHTAI.getArmyLiveNmb();

            var enableSkill = _FIGHTAI.getCanUseSkill(doactionPlayer);

            if(livenmb==1&&enableSkill.length>0){
                return _FIGHTAI.getSkillCmd(doactionPlayer,null,_CU.randomObj(enableSkill));
            }else if(livenmb<5&&enableSkill.length>0&&Math.random()<0.5){
                return _FIGHTAI.getSkillCmd(doactionPlayer,null,_CU.randomObj(enableSkill));
            }else{
                return _FIGHTAI.getNormalAttackCmd(doactionPlayer);
            }

        }

        if(_FIGHTAI.isRun(doactionPlayer)){

            return _FIGHTAI.getRunCmd(doactionPlayer);

        }else if(_FIGHTAI.useSkill(doactionPlayer)){

            var enableSkill = _FIGHTAI.getCanUseSkill(doactionPlayer);

            //console.log('随机技能组：',enableSkill);

            if(enableSkill.length>0){

                var randomskill = _CU.randomObj(enableSkill);
                var skillConf = _SKILL[randomskill];
                var tmpTargetArr = null;

                if(!skillConf.single){

                    if(skillConf.target=="self"){
                        tmpTargetArr = [doactionPlayer];
                    }else{
                        if(skillConf.islive){
                            tmpTargetArr = _FIGHT.getArmyTeamByPlayerForLive(doactionPlayer,skillConf.target=="army");
                        }else{
                            tmpTargetArr = _FIGHT.getArmyTeamByPlayer(doactionPlayer,skillConf.target=="army");
                        }
                    }

                }else{

                    var enableArr = _FIGHT.getArmyTeamByPlayer(doactionPlayer,skillConf.target=="army");
                    var aliveArr = [];
                    var deadArr = [];
                    var minper = 1;
                    var hpplayer = null;

                    for(var i=0;i<enableArr.length;i++){
                        if(enableArr[i].hp>0){
                            aliveArr.push(enableArr[i]);
                            var temproledata = _ROLEDATA.getRoleDataOfPlayer(enableArr[i]);
                            var hpper = enableArr[i].hp/temproledata.maxHp;
                            if(hpper<=minper){
                                minper=hpper;
                                hpplayer = enableArr[i];
                            }

                        }else{
                            deadArr.push(enableArr[i]);
                        }
                    }

                    if(skillConf.islive){//复活类技能优先挑选死亡队友

                        if(deadArr.length>0){
                            tmpTargetArr = [_CU.randomObj(deadArr)];
                        }else{
                            tmpTargetArr = [hpplayer];
                        }

                    }else if(skillConf.bhp||skillConf.php){//加血类技能优先选择血量比例低的队友

                        tmpTargetArr = [hpplayer];

                    }else if(skillConf.type=="attack"){//输出类技能优先挑选血量比例低的敌人

                        tmpTargetArr = [hpplayer];

                    }else{//其他技能随机存活目标
                        
                        tmpTargetArr = [_CU.randomObj(aliveArr)];
                    }

                }

                return _FIGHTAI.getSkillCmd(doactionPlayer,tmpTargetArr,randomskill);

            }
        
        }

        return _FIGHTAI.getNormalAttackCmd(doactionPlayer);
        
        

        /*
        var actioncmd = {
            player:doactionPlayer,
            //targetArr:this.getArmyTeamByPlayer(this.doactionPlayer,false),
            targetArr:_FIGHT.getArmyTeamByPlayer(doactionPlayer,true),
            //targetArr:[this.getRandomActionTarget()],
            type:'skill',
            skillName:'恐吓'
        };
        */
        
        /*
        var actioncmd={
            player:this.doactionPlayer,
            type:'run'
        }*/

    },


    isRun:function(doactionPlayer){

        
        //var playername = doactionPlayer.name;
        //var playerConf = _NPC[playername];
		var playerCha = doactionPlayer.cha;
		
		//if(playerConf&&playerConf.ftype){
		//	playerCha = playerConf.cha;
		//}
		
        if(playerCha&&_CHARACTER[playerCha]){

            var chaper = _CHARACTER[playerCha].runper;
            if(chaper){
                var playerData = _ROLEDATA.getRoleDataOfPlayer(doactionPlayer);
                var hpper = doactionPlayer.hp/playerData.maxHp;

                //console.log('逃跑概率:',chaper,hpper,hpper<=chaper);

                return hpper<=chaper;
            }

        }

        return false;

    },


    getCanUseSkill:function(doactionPlayer){

        _FIGHTAI._SKILLCACHE=[];

        var skill = _ROLEDATA.getSkillOfplayer(doactionPlayer);
        var currentmg = doactionPlayer.mg;

        if(skill&&skill.length>0){

            for(var i=0;i<skill.length;i++){
                var skillname = skill[i];
                var skillConf = _SKILL[skillname];
                if(skillConf&&skillConf.cut<=currentmg){
                    if(skillConf.skilltype=="fenshen"){
                        if(
                            (doactionPlayer.isplayer&&window.fightData.playerTeam.length<5)
                            ||(!doactionPlayer.isplayer&&window.fightData.armyTeam.length<5)
                        ){
                            _FIGHTAI._SKILLCACHE.push(skillname);
                        }else{
                            //console.log('人数过多无法使用分身');
                        }
                    }else{
                        _FIGHTAI._SKILLCACHE.push(skillname);
                    }
                }

            }

        }

        return _FIGHTAI._SKILLCACHE;
    },

    useSkill:function(doactionPlayer){

        //var playername = doactionPlayer.name;
        //var playerConf = _NPC[playername];
		var playerCha = doactionPlayer.cha;
		
		//if(playerConf){
		//	playerCha = playerConf.cha;
		//}
		
        if(playerCha&&_CHARACTER[playerCha]){

            return Math.random()<_CHARACTER[playerCha].skillper

        }

        return Math.random()<0.5;

    },

    getRunCmd:function(doactionPlayer){

        return {
            player:doactionPlayer,
            type:'run'
        };

    },

    getNormalAttackCmd:function(doactionPlayer){

        return {
            player:doactionPlayer,
            target:_FIGHT.getRandomActionTarget(),
            type:'attack'
        };

    },

    getSkillCmd:function(doactionPlayer,tmpTargetArr,skillname){

        return {
            player:doactionPlayer,
            targetArr:tmpTargetArr,
            type:'skill',
            skillName:skillname
        };

    }


}